package com.qixiaobao.web.controller.servicefee;

import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.DateUtils;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.common.utils.easyExcel.EasyExcelUtil;
import com.qixiaobao.servicefee.domain.HandlingFee;
import com.qixiaobao.servicefee.domain.HandlingFeeStatisticLog;
import com.qixiaobao.servicefee.domain.dto.*;
import com.qixiaobao.servicefee.domain.vo.HandlingFeeDetailVO;
import com.qixiaobao.servicefee.domain.vo.HandlingFeeWaitSettlementVO;
import com.qixiaobao.servicefee.service.IHandlingFeeStatisticService;
import com.qixiaobao.servicefee.service.IServiceFeeService;
import com.qixiaobao.system.domain.Person;
import com.qixiaobao.system.domain.dto.PersonDTO;
import com.qixiaobao.web.controller.core.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 手续费管理
 * @Author yangxm
 * @Date 2025/6/24 9:17
 */
@Slf4j
@RestController
@RequestMapping("/serviceFee")
public class ServiceFeeController extends BaseController {

    @Autowired
    private IServiceFeeService serviceFeeService;
    @Autowired
    private IHandlingFeeStatisticService handlingFeeStatisticService;

    /**
     * @Description 待结算列表
     * @Author yangxm
     * @Date 2025/6/24 9:40
     * @param waitSettlementDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/wait/list")
    public TableDataInfo list(WaitSettlementDTO waitSettlementDTO){
        waitSettlementDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        List<HandlingFeeWaitSettlementVO> list = serviceFeeService.selectWaitSettlementList(waitSettlementDTO);
        return getDataTable(list);
    }

    /**
     * @Description 待结算 保险费总计、手续费总计
     * @Author yangxm
     * @Date 2025/6/24 13:21
     * @param waitSettlementDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/wait/total")
    public AjaxResult moneyTotal(WaitSettlementDTO waitSettlementDTO){
        waitSettlementDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(serviceFeeService.selectWaitMoneyTotal(waitSettlementDTO));
    }

    /**
     * @Description 待结算  导出
     * @Author yangxm
     * @Date 2025/6/24 13:46
     * @param waitSettlementDTO
     * @param response
     */
    @Log(title = "手续费管理-待结算", businessType = BusinessType.EXPORT)
    @GetMapping("/wait/export")
    public void export(WaitSettlementDTO waitSettlementDTO, HttpServletResponse response){
        waitSettlementDTO.setNoPermissionSupplyIdList(checkPermission());
        List<HandlingFeeWaitSettlementVO> list = serviceFeeService.selectWaitSettlementList(waitSettlementDTO);
        try {
            EasyExcelUtil.writeExcel(response, list, HandlingFeeWaitSettlementVO.class, "手续费待结算", "sheet1");
        } catch (IOException e) {
            log.error("手续费待结算导出异常：", e);
            throw new RuntimeException("手续费待结算导出异常：" + e.getMessage());
        }
    }

    /**
     * @Description 待结算人员列表
     * @Author yangxm
     * @Date 2025/6/24 14:05
     * @param
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/wait/person/{chargeId}")
    public TableDataInfo waitPerson(@PathVariable Long chargeId){
        startPage();
        List<Person> list = serviceFeeService.selectWaitPersonList(chargeId);
        return getDataTable(list);
    }

    /**
     * @Description 待结算  - 结算功能
     * @Author yangxm
     * @Date 2025/6/24 14:11
     * @param handlingFee
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "手续费管理-待结算", businessType = BusinessType.SETTLEMENT)
    @PostMapping("/wait/settle")
    public AjaxResult waitSettlement(HandlingFee handlingFee){
        return toAjax(serviceFeeService.settleForWait(handlingFee));
    }

    /**
     * @Description 待结算  - 一键结算功能
     * @Author yangxm
     * @Date 2025/6/24 14:11
     * @param waitSettlementDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "手续费管理-待结算", businessType = BusinessType.SETTLEMENT_ALL)
    @PostMapping("/wait/settle/all")
    public AjaxResult waitAllSettlement(@RequestBody WaitSettlementDTO waitSettlementDTO){
        return toAjax(serviceFeeService.settleAllForWait(waitSettlementDTO));
    }

    /**
     * @Description 待收款、已结算 列表
     * handlingFeeStatus: 0 待收款 1 已结算
     * @Author yangxm
     * @Date 2025/6/24 14:11
     * @param handlingFeeDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/handlingFee/list")
    public TableDataInfo handlingFeeList(HandlingFeeDTO handlingFeeDTO){
        handlingFeeDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        List<HandlingFee> list = serviceFeeService.selectHandlingFeeList(handlingFeeDTO);
        return getDataTable(list);
    }

    /**
     * @Description 待收款、已结算  详情
     * @Author yangxm
     * @Date 2025/6/25 13:12
     * @param handlingFeeDetailDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/handlingFee/detail")
    public TableDataInfo handlingFeeDetail(HandlingFeeDetailDTO handlingFeeDetailDTO){
        if (handlingFeeDetailDTO.getHandlingFeeId() == null){
            throw new UtilException("参数错误");
        }
        startPage();
        List<HandlingFeeDetailVO> list = serviceFeeService.selectHandlingFeeDetailList(handlingFeeDetailDTO);
        return getDataTable(list);
    }

    /**
     * @Description 待收款、已结算  详情导出
     * @Author yangxm
     * @Date 2025/6/25 13:12
     * @param handlingFeeDetailDTO
     * @param response
     */
    @GetMapping("/handlingFee/detail/export")
    public void handlingFeeDetailExport(HandlingFeeDetailDTO handlingFeeDetailDTO, HttpServletResponse response){
        if (handlingFeeDetailDTO.getHandlingFeeId() == null){
            throw new UtilException("参数：手续费ID 错误");
        }
        if (StringUtils.isBlank(handlingFeeDetailDTO.getHandlingFeeStatus())){
            throw new UtilException("参数：手续费状态 错误");
        }
        String fileName = "手续费待结算详情";
        if (StringUtils.equals("1", handlingFeeDetailDTO.getHandlingFeeStatus())){
            fileName = "手续费已结算详情";
        }
        List<HandlingFeeDetailVO> list = serviceFeeService.selectHandlingFeeDetailList(handlingFeeDetailDTO);
        try {
            EasyExcelUtil.writeExcel(response, list, HandlingFeeDetailVO.class, fileName, "sheet1");
        } catch (IOException e) {
            log.error(fileName + "导出异常：", e);
            throw new UtilException(fileName + "导出异常：" + e.getMessage());
        }
    }

    /**
     * @Description 待收款、已结算  - 详情 - 人员列表
     * @Author yangxm
     * @Date 2025/6/25 13:12
     * @param personDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/handlingFee/detail/person/list")
    public TableDataInfo handlingFeeDetailPersonList(PersonDTO personDTO){
        if (personDTO.getChargeId() == null){
            throw new UtilException("参数错误");
        }
        startPage();
        List<Person> list = serviceFeeService.selectHandlingFeeDetailPersonList(personDTO);
        return getDataTable(list);
    }

    /**
     * @Description 待收款、已结算 导出
     * @Author yangxm
     * @Date 2025/6/24 14:11
     * @param handlingFeeDTO
     * @param response
     */
    @Log(title = "手续费管理-待收款、已结算", businessType = BusinessType.EXPORT)
    @GetMapping("/handlingFee/export")
    public void export(HandlingFeeDTO handlingFeeDTO, HttpServletResponse response){
        if (StringUtils.isBlank(handlingFeeDTO.getHandlingFeeStatus())){
            throw new UtilException("参数：手续费状态 错误");
        }
        handlingFeeDTO.setNoPermissionSupplyIdList(checkPermission());
        serviceFeeService.handlingFeeExport(handlingFeeDTO, response);
    }

    /**
     * @Description 待收款、已结算  - 修改备注
     * @Author yangxm
     * @Date 2025/6/25 13:12
     * @param
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @Log(title = "手续费管理-待收款、已结算", businessType = BusinessType.UPDATE)
    @PostMapping("/handlingFee/updateRemark")
    public AjaxResult updateRemark(@RequestBody HandlingFee handlingFee){
        return toAjax(serviceFeeService.updateRemark(handlingFee));
    }

    /**
     * @Description 手续费汇总
     * @Author yangxm
     * @Date 2025/6/25 15:39
     * @param handlingFeeStatisticDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/handlingFee/statistic/list")
    public TableDataInfo handlingFeeStatisticList(HandlingFeeStatisticDTO handlingFeeStatisticDTO){
        if (StringUtils.isBlank(handlingFeeStatisticDTO.getYearMonth())){
            throw new IllegalArgumentException("参数：汇总月份不能为空！");
        }
        //判断查询是否是当月数据
        String yearMonth = DateUtils.dateTimeNow(DateUtils.YYYY_MM);
        //当月实时发票汇总数据
        if (yearMonth.equals(handlingFeeStatisticDTO.getYearMonth())){
            //上一个汇总日期
            String lastYearMonth = DateUtils.adjustMonth(yearMonth, -1);
            handlingFeeStatisticDTO.setLastYearMonth(lastYearMonth);
            startPage();
            return getDataTable(handlingFeeStatisticService.selectMonthHandlingFeeStatisticList(handlingFeeStatisticDTO));
        }else {
            startPage();
            return getDataTable(handlingFeeStatisticService.selectHandlingFeeStatisticList(handlingFeeStatisticDTO));
        }
    }

    /**
     * @Description 手续费汇总 明细
     * @Author yangxm
     * @Date 2025/6/25 16:31
     * @param handlingFeeStatisticDetailDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/handlingFee/statistic/detail")
    public AjaxResult handlingFeeStatisticDetail(HandlingFeeStatisticDetailDTO handlingFeeStatisticDetailDTO){
        //当前月份
        if (handlingFeeStatisticDetailDTO.getHandlingFeeStatisticId() == null){
            if (StringUtils.isBlank(handlingFeeStatisticDetailDTO.getReceiptCompany()) || StringUtils.isBlank(handlingFeeStatisticDetailDTO.getSupplyName())){
                throw new UtilException("参数: 手续费收款公司 或 供应商不能为空！");
            }
            return AjaxResult.success(handlingFeeStatisticService.selectMonthHandlingFeeStatisticDetail(handlingFeeStatisticDetailDTO));
            //历史月份汇总明细
        }else {
            return AjaxResult.success(handlingFeeStatisticService.selectHandlingFeeStatisticDetail(handlingFeeStatisticDetailDTO));
        }
    }

    /**
     * @Description 手续费汇总 - 实收明细
     * @Author yangxm
     * @Date 2025/6/25 16:57
     * @param handlingFeeStatisticDetailDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/handlingFee/statistic/receiptDetail")
    public TableDataInfo receiptHandlingFeeDetail(HandlingFeeStatisticDetailDTO handlingFeeStatisticDetailDTO){
        if (StringUtils.isBlank(handlingFeeStatisticDetailDTO.getYearMonth())){
            throw new UtilException("参数：汇总月份不能为空！");
        }
        if (StringUtils.isBlank(handlingFeeStatisticDetailDTO.getSupplyName())){
            throw new UtilException("参数: 供应商不能为空！");
        }
        handlingFeeStatisticDetailDTO.setYearMonth(handlingFeeStatisticDetailDTO.getYearMonth() + "-01");
        startPage();
        return getDataTable(handlingFeeStatisticService.selectReceiptHandlingFeeDetailList(handlingFeeStatisticDetailDTO));
    }

    /**
     * @Description 手续费汇总 - 记录
     * @Author yangxm
     * @Date 2025/6/26 10:39
     * @param handlingFeeStatisticLog
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/handlingFee/statistic/log")
    public TableDataInfo handlingFeeStatisticLogList(HandlingFeeStatisticLog handlingFeeStatisticLog){
        if (handlingFeeStatisticLog.getHandlingFeeStatisticId() == null){
            throw new UtilException("参数：手续费汇总ID不能为空");
        }
        startPage();
        return getDataTable(handlingFeeStatisticService.selectHandlingFeeStatisticLogList(handlingFeeStatisticLog));
    }
}
